<?php
/**
* File that contains Application_Form_Admin_editColor class.
* @package busmap
*/

/**
* Application_Form_Admin_editColor class generates a form used to edit colors.
* Form has a text field for the color name.
* There are thee text fields for the red, blue and green values of the color.
* There is also a disabled text field with the background color set to the color of the corresponding color.
* @package busmap
*/
class Application_Form_Admin_editColor extends Zend_Form
{
	/**
	* Initialize function generates all the elements described in the class description.
	*/
    public function init()
    {
        $this->setMethod('post');

        $this->addElement(
            'hidden', 'id', array(
                'required' => true
            ));
 
        $this->addElement(
            'text', 'name', array(
                'required' => true,
                'filters'    => array('StringTrim'),
				'class' => 'bold-text'
            ));
		$this->name->addErrorMessage('Värvi nimi ei saa olla tühi');
		$this->name->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));

        $this->addElement(
            'text', 'redValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->redValue->addErrorMessage('Punane toon peab olema number');
		$this->redValue->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell'));

		$this->addElement(
            'text', 'greenValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->greenValue->addErrorMessage('Roheline toon peab olema number');
		//$this->greenValue->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));

		 $this->addElement(
            'text', 'blueValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->blueValue->addErrorMessage('Sinine toon peab olema number');
		$this->blueValue->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell'));

		 $this->addElement(
            'text', 'color', array(
                'required' => false,
				'disabled' => true
            ));
		$this->color->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));

		 $this->addElement(
            'submit', 'change', array(
                'required' => false,
				'label' => 'Muuda',
				'class' => 'submit-data'
            ));
		$this->change->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));

		 $this->addElement(
            'submit', 'delete', array(
                'required' => false,
				'label' => 'Kustuta',
				'class' => 'delete-data'
            ));
		$this->delete->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));
 
		$this->clearElementLabels();
		
    }

	/**
	* This function overwrites the Zend_Form isValid method.
	* It sends the data to the parent's isValid method too, but also checks if the inserted red, green and blue value are between 0 and 255.
	*/
	public function isValid($form){
		if(!$form) return;

		$valid = parent::isValid($form);

		if(array_key_exists('redValue', $form) && $form['redValue'] > 255 || $form['redValue'] < 0){
			$this->redValue->addError('Punase tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}

		if(array_key_exists('greenValue', $form) && $form['greenValue'] > 255 || $form['greenValue'] < 0){
			$this->greenValue->addError('Rohelise tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}

		if(array_key_exists('blueValue', $form) && $form['blueValue'] > 255 || $form['blueValue'] < 0){
			$this->blueValue->addError('Sinise tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}
		return $valid;

	}

	/**
	* This function sets the disabled text field's background color to the color sent to it.
	*/
	public function setColorField($red, $green, $blue){
		$this->color->setAttrib('style', 'background-color:rgb('.intval($red).','.intval($green).','.intval($blue).')');
	}


	/**
	* This function is called when the delete request is received.
	* It removes the delete button and generates two new buttons. One being the delete confirmation button that upon sending to the controller really deletes the entry.
	*/
	public function askDeleteConfirmation(){
		$this->removeElement('delete');

		$this->addElement('submit', 'back', array(
			 'ignore'   => true,
			 'label'	=>'Ära kustuta',
			 'class'	=>'submit-data'
		));
		$this->back->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell-button'));
		
		$this->addElement('submit', 'delete_confirm', array(
			 'ignore'   => true,
			 'label'	=>'Kustuta! [Kinnitus]',
			 'class'	=>'delete-data'
		));
		$this->delete_confirm->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell-button delete-data'));

		$this->clearElementLabels();

	}

	/**
	* This function clears the label, DtDd and Error decorators from all the form elements.
	* Errors are being shown elsewhere in the view.
	*/
	public function clearElementLabels(){
		$elements = $this->getElements();
		foreach($elements as $element){
			$element->removeDecorator('label')->removeDecorator('DtDdWrapper')->removeDecorator('Errors');
		}
	}

}